using System;
using System.Windows.Forms;
using System.Data.SQLite;

namespace LoginSystem
{
    public partial class RegisterForm : Form
    {
        private readonly string connectionString;

        public RegisterForm(string connString)
        {
            connectionString = connString;
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.Text = "用户注册";
            this.Width = 400;
            this.Height = 350;
            this.StartPosition = FormStartPosition.CenterParent;

            // 用户名标签
            Label lblUsername = new Label();
            lblUsername.Text = "用户名：";
            lblUsername.Location = new System.Drawing.Point(50, 50);
            lblUsername.AutoSize = true;

            // 用户名输入框
            TextBox txtUsername = new TextBox();
            txtUsername.Name = "txtUsername";
            txtUsername.Location = new System.Drawing.Point(150, 50);
            txtUsername.Width = 200;

            // 密码标签
            Label lblPassword = new Label();
            lblPassword.Text = "密码：";
            lblPassword.Location = new System.Drawing.Point(50, 90);
            lblPassword.AutoSize = true;

            // 密码输入框
            TextBox txtPassword = new TextBox();
            txtPassword.Name = "txtPassword";
            txtPassword.Location = new System.Drawing.Point(150, 90);
            txtPassword.Width = 200;
            txtPassword.PasswordChar = '*';

            // 确认密码标签
            Label lblConfirmPassword = new Label();
            lblConfirmPassword.Text = "确认密码：";
            lblConfirmPassword.Location = new System.Drawing.Point(50, 130);
            lblConfirmPassword.AutoSize = true;

            // 确认密码输入框
            TextBox txtConfirmPassword = new TextBox();
            txtConfirmPassword.Name = "txtConfirmPassword";
            txtConfirmPassword.Location = new System.Drawing.Point(150, 130);
            txtConfirmPassword.Width = 200;
            txtConfirmPassword.PasswordChar = '*';

            // 注册按钮
            Button btnRegister = new Button();
            btnRegister.Text = "注册";
            btnRegister.Location = new System.Drawing.Point(150, 180);
            btnRegister.Width = 100;
            btnRegister.Click += new EventHandler(BtnRegister_Click);

            // 添加控件到窗体
            this.Controls.AddRange(new Control[] {
                lblUsername, txtUsername,
                lblPassword, txtPassword,
                lblConfirmPassword, txtConfirmPassword,
                btnRegister
            });
        }

        private void BtnRegister_Click(object sender, EventArgs e)
        {
            string username = Controls.Find("txtUsername", true)[0].Text;
            string password = Controls.Find("txtPassword", true)[0].Text;
            string confirmPassword = Controls.Find("txtConfirmPassword", true)[0].Text;

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(confirmPassword))
            {
                MessageBox.Show("所有字段都必须填写！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (password != confirmPassword)
            {
                MessageBox.Show("两次输入的密码不一致！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (RegisterUser(username, password))
            {
                MessageBox.Show("注册成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        private bool RegisterUser(string username, string password)
        {
            try
            {
                using (var connection = new SQLiteConnection(connectionString))
                {
                    connection.Open();
                    string sql = "INSERT INTO Users (Username, Password) VALUES (@Username, @Password)";
                    using (var command = new SQLiteCommand(sql, connection))
                    {
                        command.Parameters.AddWithValue("@Username", username);
                        command.Parameters.AddWithValue("@Password", password);
                        command.ExecuteNonQuery();
                        return true;
                    }
                }
            }
            catch (SQLiteException)
            {
                MessageBox.Show("用户名已存在！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"注册失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
    }
}